<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!--0:引入 vue.js-->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <style>
      .msg {
        display: inline-block;
        width: 180px;
        height: 25px;
        border: 1px solid #555;
        text-align: center;
        line-height: 25px;
      }

      .success {
        background-color: cadetblue;
      }

      .fail {
        background-color: crimson;
      }
    </style>
  </head>
  <body>
    <!--界面-->
    <div id="app">
      <!--输入电话号码-->
      <input type="text" v-model="phone" />
      <span class="msg" :class="myMsg">{{msg}}</span>
    </div>

    <script>
      var vm = new Vue({
        //界面
        el: "#app",
        //数据
        data: {
          phone: "", //电话号码
          msg: "", //提示信息
          myMsg: {
            success: false,
            fail: false,
          },
        },
        //方法
        methods: {},

        watch: {
          phone() {
            var reg = /^1[3-9]\d{9}$/; //定义验证正则表达式
            //判断
            if (reg.test(this.phone) == true) {
              this.myMsg.success = true;
              this.myMsg.fail = false;
              this.msg = "电话号码是正确的！";
            } else {
              this.myMsg.success = false;
              this.myMsg.fail = true;
              this.msg = "电话码号是不正确的！";
            }
          },
        },
      });
    </script>
  </body>
</html>
